//
//  CHLabel.swift
//  jyzx
//
//  Created by cyptt on 2022/6/6.
//

import UIKit

extension UILabel {

    /*
     间距"
     @lineH 间距
     @textColor 字体颜色
     @font 字体
     @firstLineHeadIndent 段落首行缩减
     @keyKern 字体间距
     */
    public  func setLineH(lineH:CGFloat = 10,textColor:UIColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 51/255.0),font:UIFont = UIFont.systemFont(ofSize: 14),firstLineHeadIndent:CGFloat = 0,keyKern:CGFloat? = nil){
        
        let mutAttString:NSMutableAttributedString = NSMutableAttributedString.init(string: self.text ?? "")
        
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineH
        paragraphStyle.firstLineHeadIndent = firstLineHeadIndent
    
   
        
        mutAttString.addAttributes(
            [
                NSAttributedString.Key.paragraphStyle:paragraphStyle,
                NSAttributedString.Key.foregroundColor:textColor,
                NSAttributedString.Key.font:font
            ], range: NSMakeRange(0, mutAttString.length))
        if keyKern != nil{
            mutAttString.addAttributes(
                [
                    NSAttributedString.Key.kern:keyKern!
                ], range: NSMakeRange(0, mutAttString.length))
        }
        
        self.attributedText = mutAttString;
    }
    
   
    
    /*
     计算label高度传参数
     */
    public func labelHeight(width:CGFloat,fontSize:CGFloat,text:String)->CGFloat{
        let dic = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: fontSize)]
        let size = CGSize(width: width, height: CGFloat(MAXFLOAT))
        let rect = text.boundingRect(with: size, options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: dic, context: nil)
        return CGFloat(ceilf(Float(rect.size.height)))
    }
    
    /*
     计算label 高度 不传参数
     */
    public func labelHeight()->CGFloat{
        let dic = [NSAttributedString.Key.font : self.font]
        let size = CGSize(width: self.frame.size.width, height: CGFloat(MAXFLOAT))
        let rect = self.text!.boundingRect(with: size, options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: dic, context: nil)
        return CGFloat(ceilf(Float(rect.size.height)))
    }
    
    /*
     计算label 宽度
     */
    public  func labelWidth()->CGFloat{
        let dic = [NSAttributedString.Key.font : self.font]
        let size = CGSize(width: CGFloat(MAXFLOAT), height: self.frame.size.height)
        let rect = self.text!.boundingRect(with: size, options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: dic, context: nil)
        return CGFloat(ceilf(Float(rect.size.width)))
    }
    
    /**
     设置局部字体颜色
     @param allText 全文
     @partText partText 需要处理的文字

     */
    public func attributedText(allText:String,partText:String,color:UIColor,font:UIFont,oriFont:UIFont? = nil,oriColor:UIColor? = nil){
        var oriFontM = oriFont
        var oriColorM = oriColor
        if oriFontM == nil{
            oriFontM = self.font
        }
        if oriColorM == nil{
            oriColorM = self.textColor
        }
        let mutAttString:NSMutableAttributedString = NSMutableAttributedString().partText(allText: allText, partText: partText, color: color, font: font,oriFont: oriFontM,oriColor: oriColorM)
        
        self.attributedText = mutAttString

    }
}
